﻿using FreeSql.Internal.Model;
using Mohe.Scada.Util.Exceptions;
using Mohe.Scada.Util.Extensions;
using Mohe.Scada.Util.Models;
using System.Linq.Expressions;

namespace Mohe.Scada.WebApi.Services
{
    public class BaseCurdService<T> : ICurdService<T> where T : class
    {
        protected readonly IFreeSql appDb;

        public BaseCurdService(IFreeSql appDb)
        {
            this.appDb = appDb;
        }


        public virtual bool Add(T model)
        {
            return appDb.Insert(model).ExecuteAffrows() > 0;
        }

        public virtual bool Delete(object id)
        {
            var entity = appDb.Select<T>(id).ToOne();
            if (entity is null)
                throw new CustomException("根据Id 查询不到数据");

            return appDb.Delete<T>(id).ExecuteAffrows() > 0;
        }

        public virtual bool Edit(object id, ParameterModel model)
        {
            var entity = appDb.Select<T>(id).ToOne();
            if (entity is null)
                throw new CustomException("根据Id 查询不到数据");

            entity.TryFromDict(model);
            return appDb.Update<T>().SetSource(entity).ExecuteAffrows() > 0;
        }

        public virtual T Get(object id)
        {
            return appDb.Select<T>(id).ToOne();
        }

        public virtual T Get(Expression<Func<T, bool>> where)
        {
            var entity = appDb.Select<T>().Where(where).ToOne();

            if (entity is null)
                throw new CustomException("根据Id 查询不到数据");

            return entity;
        }

        public virtual T Get(DynamicFilterInfo filters)
        {
            return appDb.Select<T>().WhereDynamicFilter(filters).ToOne();
        }

        public virtual List<T> List(DynamicFilterInfo filters, int maxRows = 50000)
        {
            return appDb.Select<T>().WhereDynamicFilter(filters).Take(maxRows).ToList();
        }

        public virtual List<T> List(Expression<Func<T, bool>> where, int maxRows = 50000)
        {
            return appDb.Select<T>().Where(where).Take(maxRows).ToList();
        }

        public virtual ApiPageResult Page(PageRequest pageRequest)
        {
            bool isAsc = "desc".Equals(pageRequest.SortType) ? false : true;
            var list = appDb.Select<T>().WhereDynamicFilter(pageRequest.Filters);
            var page = list.Skip((pageRequest.Page - 1) * pageRequest.Size)
                        .Take(pageRequest.Size)
                        .OrderByPropertyNameIf(!string.IsNullOrEmpty(pageRequest.SortField), pageRequest.SortField, isAsc)
                        .ToList();
            return ApiPageResult.Success(page, list.Count());
        }

        public virtual bool SetFlag(object id, ParameterModel model)
        {
            var entity = appDb.Select<T>(id).ToOne();
            if (entity is null)
                throw new CustomException("根据Id 查询不到数据"); ;
            var update = Activator.CreateInstance<T>();
            update.TryFromDict(model);
            return appDb.Update<T>().SetSource(update).WhereDynamic(id).ExecuteAffrows() > 0;
        }
    }
}
